<?php

namespace app\admin\model;

use think\Model;


class Dispatch extends Model
{


    // 表名
    protected $name = 'dispatch';
    //    public $table = 'nsig_project';
    // 自动写入时间戳字段
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'framework_agreement_time_text',
        'invesment_agreement_time_text',
        'tmp_framework_agreement_time_text',
        'tmp_invesment_agreement_time_text',


    ];

    protected static function init()
    {


        Dispatch::event('before_update',function ($data){
           $data->updateYear($data);
        });
    }

    protected function updateYear($data){

        $maxtime = max($data['framework_agreement_time'],$data['invesment_agreement_time'],$data['tmp_framework_agreement_time'],$data['tmp_invesment_agreement_time']);
        if (is_numeric($maxtime))
            $data['year'] = date("Y",$maxtime);
    }
    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getFrameworkAgreementTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['framework_agreement_time']) ? $data['framework_agreement_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }
    public function getInvesmentAgreementTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['invesment_agreement_time']) ? $data['invesment_agreement_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }
    public function getTmpFrameworkAgreementTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['tmp_framework_agreement_time']) ? $data['tmp_framework_agreement_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }
    public function getTmpInvesmentAgreementTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['tmp_invesment_agreement_time']) ? $data['tmp_invesment_agreement_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setInvesmentAgreementTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    protected function setFrameworkAgreementTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setTmpInvesmentAgreementTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    protected function setTmpFrameworkAgreementTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

}
